//////////// reflection
window.woody = {};

Function.prototype.method = function (name, func) {
  this.prototype[name] = func;
  return this;
};

Function.prototype.inherits = function (Parent) {
  this.prototype = new Parent();
  return this;
};

//////////// base woody

woody.log = {};
woody.log.debug = function (msg) {
  if (console && console.debug) {
    console.debug(msg);
  }
};

woody.jersey = {
  array:function (obj, name) {
    obj[name] = obj[name] || [];
  }
};

woody.utils = {
  isArray:function (obj) {
    return obj.length !== undefined;
  }
}

woody.dom = {
  addDiv:function (container, className, id) {
    var div = document.createElement('div');
    if (className) {
      div.className = className;
    }
    //    if (id) {
    //      div.id = id;
    //    }
    return container.appendChild(div);
  },
  show:function (elem, type) {
    elem.style.display = type || 'block';
  },
  hide:function (elem) {
    elem.style.display = 'none';
  },
  visible:function (elem, type) {
    elem.style.visibility = type || 'visible';
  },
  invisible:function (elem) {
    elem.style.visibility = 'hidden';
  }
};

//////////// schema

var deberc = {};

deberc.schema = {
  MAIN_ROOM_REFRESH_INTERVAL:60000,
  BASE_REST_PATH:''
};

//////////// common controls

deberc.view = {};

deberc.view.Button = function (id, context, handler) {
  document.getElementById(id).onclick = function () {
    handler.call(context);
  }
};

(function () {
  var _context;
  var _handler;
  var _container;
  deberc.view.Table = function (container, context, handler) {
    _container = container;
    _context = context;
    _handler = handler;
  }
    .method('clear',
    function () {
      _container.innerHTML = '';
    })
    .method('add',
    function (data, handler) {
      var inner = '<div class="c link wn">' + data.id +
        '</div><div class="c wp">' + data.data.split(';')[0] +
        '</div><div class="c wd">' + 'asd' +
        '</div><div class="c link wa">Войти</div><div class="b"></div>';
      var row = woody.dom.addDiv(_container, 'r');
      row.innerHTML = inner;
      row.onclick = handler;
    });
})();

//////////// controls

deberc.rest = {};

deberc.rest.session = {
  userId:Math.round((Math.random() * 10000)),
  isCurrent:function (id) {
    return this.userId.toString() === id.toString();
  }
};

// TODO: errors and timeouts callbacks
deberc.rest.manager = {
  createRandomParam:function () {
    return 'r=' + Math.random();
  },
  getRooms:function (context, callBack) {
    $.ajax({
      url:deberc.schema.BASE_REST_PATH + 'rest/main_room/rooms?' + this.createRandomParam(),
      context:context,
      success:function (response, status, xhr) {
        woody.jersey.array(response, 'rooms');
        callBack.call(context, response);
      }
    });
  },
  createRoom:function () {
    $.ajax({
      url:deberc.schema.BASE_REST_PATH + 'rest/main_room/create?player_id=' +
        deberc.rest.session.userId + '&' + this.createRandomParam(),
      context:document,
      success:function (response, status, xhr) {
        deberc.gameRoom.enter(response.roomId)
      }
    });
  },
  enterRoom:function (roomId, scope, callback) {
    $.ajax({
      url:deberc.schema.BASE_REST_PATH + 'rest/main_room/get_token?player_id=' +
        deberc.rest.session.userId + '&id=' + roomId + '&' + this.createRandomParam(),
      context:document,
      success:function (response, status, xhr) {
        callback.call(scope, response.token);
      }
    });
  },
  move:function (roomId, move, scope, callback) {
    $.ajax({
      url:deberc.schema.BASE_REST_PATH + 'rest/main_room/move?player_id=' +
        deberc.rest.session.userId + '&id=' + roomId + '&move=' + encodeURI(move) + '&' + this.createRandomParam(),
      context:document,
      success:function (response, status, xhr) {
        callback.call(scope, response);
      }
    });
  }
};
//
//$.cometd.configure({
//  url: "cometd"
//  //  ,logLevel: 'debug'
//});

